﻿using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace Web.Context.Analysis
{
    public class WebAccessHelper
    {
        public HtmlDocument GetHtmlDocmentByUrl(string urlStr, bool isReplaceInHis, WebBrowser wb)
        {
            var myReq = (HttpWebRequest) WebRequest.Create(urlStr);
            var myResp = (HttpWebResponse) myReq.GetResponse();
            Stream rStream = myResp.GetResponseStream();
            if (rStream == null) return null;
            var respStream = new StreamReader(rStream, Encoding.Default);
            string strhtml;
            try
            {
                strhtml = respStream.ReadToEnd();
            }
            finally
            {
                respStream.Dispose();
                respStream.Close();
                myResp.Close();
            }
            if (string.IsNullOrEmpty(strhtml)) return null;
            wb.Navigate("about:blank");
            wb.Document.OpenNew(isReplaceInHis);
            wb.Document.Write(strhtml);
            return wb.Document;
        }
    }
}